package com.cskaoyan.service.wxfree;

import com.cskaoyan.bean.goods.Goods;
import com.cskaoyan.bean.user.WxUserLoginData;
import com.cskaoyan.bean.wxfree.WxFootprint;
import com.cskaoyan.bean.wxfree.WxFootprintExample;
import com.cskaoyan.bo.wxfree.WxFpDeleteBo;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.WxFootprintMapper;
import com.cskaoyan.vo.wxfree.WxFootprintListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: mall
 * @description:
 * @author: Mr.Mengmeng
 * @create: 2021-04-12 09:25
 **/
// 微信端足迹模块
@Service
public class WxFootprintServiceImpl implements WxFootprintService {
    @Autowired
    WxFootprintMapper wxFootprintMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public WxFootprintListVo fpList(Integer page, Integer size) {
        Subject subject = SecurityUtils.getSubject();
        WxUserLoginData loginData = (WxUserLoginData) subject.getPrincipal();
        Integer userId = loginData.getId();
        // 分页
        PageHelper.startPage(page, size);
        // 查询全部足迹的条件
        WxFootprintExample wxFtExample = new WxFootprintExample();
        wxFtExample.setOrderByClause("add_time" + " " + "desc");
        WxFootprintExample.Criteria criteria = wxFtExample.createCriteria();
        criteria.andDeletedEqualTo(false).andUserIdEqualTo(userId);
        // 查询
        List<WxFootprint> wxFootprints = wxFootprintMapper.selectByExample(wxFtExample);

        // 通过每条足迹信息的商品id，去获得商品信息
        ArrayList<WxFootprintListVo.FootprintListBean> footprintListBeans = new ArrayList<>();
        for (WxFootprint wxFootprint : wxFootprints) {
            Goods goods = goodsMapper.selectByPrimaryKey(wxFootprint.getGoodsId());

            //
            WxFootprintExample wxFootprintExample = new WxFootprintExample();
            wxFootprintExample.createCriteria().andGoodsIdEqualTo(goods.getId()).andDeletedEqualTo(false);
            List<WxFootprint> wxFootprints1 = wxFootprintMapper.selectByExample(wxFootprintExample);
            WxFootprint wxFootprint1 = wxFootprints1.get(0);

            WxFootprintListVo.FootprintListBean footprintListBean = new WxFootprintListVo.FootprintListBean(goods.getBrief(),
                    goods.getPicUrl(), wxFootprint1.getUpdateTime(), goods.getId(), goods.getName(), wxFootprint.getId(), goods.getRetailPrice());
            footprintListBeans.add(footprintListBean);
        }

        // 总页数
        PageInfo<WxFootprint> wxFootprintPageInfo = new PageInfo<>(wxFootprints);
        int pages = wxFootprintPageInfo.getPages();

        return new WxFootprintListVo(pages, footprintListBeans);
    }

    @Override
    public void fpDelete(WxFpDeleteBo bo) {
        WxFootprint wxFootprint = new WxFootprint();
        wxFootprint.setId(bo.getId());
        wxFootprint.setDeleted(true);
        wxFootprintMapper.updateByPrimaryKeySelective(wxFootprint);
    }

    @Override
    public void insertFootprint(Integer goodsId) {
        Subject subject = SecurityUtils.getSubject();
        WxUserLoginData loginData = (WxUserLoginData) subject.getPrincipal();
        Integer userId = loginData.getId();

        // 判断是否曾经浏览过的商品，如果有，只需要更新时间即可
        WxFootprintExample wxFootprintExample = new WxFootprintExample();
        wxFootprintExample.createCriteria().andDeletedEqualTo(false).andGoodsIdEqualTo(goodsId).andUserIdEqualTo(userId);
        List<WxFootprint> wxFootprints = wxFootprintMapper.selectByExample(wxFootprintExample);

        if(wxFootprints.size()!=0){
            WxFootprint wxFootprint1 = wxFootprints.get(0);
            wxFootprint1.setUpdateTime(new Date());
            wxFootprintMapper.updateByPrimaryKeySelective(wxFootprint1);
        }else {
            WxFootprint wxFootprint = new WxFootprint(null, userId, goodsId, new Date(), new Date(), false);
            wxFootprintMapper.insertSelective(wxFootprint);
        }


    }
}
